//
//  SmileButton.m
//  ebook
//
//  Created by 吴超飞 on 16/5/8.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "SmileButton.h"
//#import "UIButton+DragButton.h"

#define INFOTEXT @"喜欢51小说吗？期待您的五星好评喔~"

@interface SmileButton()

@property (nonatomic,retain) UILabel *infoLabel;

@end

@implementation SmileButton

-(instancetype)initWithFrame:(CGRect)frame{
   
    self = [super initWithFrame:frame];
   
    if (self) {
        
        NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"animation_star_01"],[UIImage imageNamed:@"animation_star_02"], nil];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        imageView.animationImages = array; //动画图片数组
        imageView.animationDuration = 3.0; //执行一次完整动画所需的时长
        imageView.animationRepeatCount = 0;  //动画重复次数 0表示无限次，默认为0
        [imageView startAnimating];
        imageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(tapOnContentView:)];
        [imageView addGestureRecognizer:tapGestureRecognizer];
        
        self.backgroundColor = [UIColor clearColor];
        self.layer.cornerRadius = 8;
        [self addSubview:imageView];
        
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.backgroundColor  = [UIColor clearColor];
        _infoLabel.textColor = MTRateGoldColor;
        _infoLabel.text = INFOTEXT;
        _infoLabel.font = [UIFont fontWithName:@"Courier" size:16];
    }
    
    return self;
}

/**
 *显示提示
 */
-(void)showInfoLabel{
   
    CGSize size = [INFOTEXT sizeWithAttributes:@{NSFontAttributeName:_infoLabel.font}];
    CGRect startFrame = CGRectMake(0,  (self.frame.size.height - size.height)/2, size.width, size.height);
    
    CGRect frame = startFrame;
    frame.origin.x = -size.width;
    
    _infoLabel.frame = startFrame;
    _infoLabel.alpha = 1.0;
    [self insertSubview:_infoLabel atIndex:0];
    
    [UIView animateWithDuration:1.0 animations:^{
        _infoLabel.frame = frame;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2.0 delay:8.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            _infoLabel.alpha = 0;
        } completion:^(BOOL finished) {
            
        }];
    }];
}

-(void)tapOnContentView:(UITapGestureRecognizer *)tap{
    
    if ([self.delegate respondsToSelector:@selector(smileButtonClick:)]) {
        [self.delegate smileButtonClick:self];
    }
}

@end
